﻿using UnityEngine;
using System.Collections;
[RequireComponent(typeof(Rigidbody))]
public class BoatMovementController : MonoBehaviour {

    public float vertical = 0;
    public float horizontal = 0;
    public float acceleration = 0.1f;
    public float RotationAngle = 100;
    public float minSpeed = 1;
    public Vector3 seaForce = new Vector3(10, 0, 0);
    BoatAnimationController animationController = null;
    

	// Use this for initialization
	void Start () {
        animationController = GetComponent<BoatAnimationController>();
	}
	
	// Update is called once per frame
	void Update () {
        
       // float vertical = Input.GetAxis("Vertical");
       // float horizontal = Input.GetAxis("Horizontal");

        if (vertical > 0.1f)
            rigidbody.AddForce(transform.forward * acceleration * vertical);
        else if(animationController != null && horizontal == 0)
        {
            Vector3 f = new Vector3();
            switch (animationController.angleIndex)
            {
                case BoatAnimationController.N:
                    f = new Vector3(0, 0, 1).normalized;
                    break;
                case BoatAnimationController.NE:
                    f = new Vector3(1, 0, 1).normalized;
                    break;
                case BoatAnimationController.E:
                    f = new Vector3(1, 0, 0).normalized;
                    break;
                case BoatAnimationController.SE:
                    f = new Vector3(1, 0, -1).normalized;
                    break;
                case BoatAnimationController.S:
                    f = new Vector3(0, 0, -1).normalized;
                    break;
                case BoatAnimationController.SW:
                    f = new Vector3(-1, 0, -1).normalized;
                    break;
                case BoatAnimationController.W:
                    f = new Vector3(-1, 0, 0).normalized;
                    break;
                case BoatAnimationController.NW:
                    f = new Vector3(-1, 0, 1).normalized;
                    break;
            }
            transform.forward = f;
        }
        if (Mathf.Abs(horizontal) > 0.1f)
            rigidbody.MoveRotation(rigidbody.rotation * (Quaternion.Euler(0, RotationAngle * Time.deltaTime * horizontal, 0)));
        if (rigidbody.velocity.magnitude < minSpeed)
        {
            rigidbody.AddForce(seaForce);
        }
	}
}
